<?php
if ($_SESSION['login_user']['status'] != 'admin' || LZ_MODULE != 'admin') die('Access Denied');
if($_SESSION['login_user']['status'] != 'admin')
{
	lz_exit(ACCESS_DENIED,'admin.php',1);
}
include_once('model/photo.php');
$obj = new LZ_Photo;
$m = $_GET['m'];
$id = intval($_GET['id']);
$ids = $_GET['ids'];
$time = $_GET['time'];
$page = $_GET['page'];
$from = $_GET['from'];
$to = $_GET['to'];
$keyword = $_GET['keyword'];

$_GET['order_by'] && $_SESSION['photo_order_by'] = $_GET['order_by'];
$_GET['order_asc'] && $_SESSION['photo_order_asc'] = $_GET['order_asc'];
!$_SESSION['photo_order_by'] && $_SESSION['photo_order_by'] = 'update_time';
!$_SESSION['photo_order_asc'] && $_SESSION['photo_order_asc'] = 'DESC';
$order_by = $_SESSION['photo_order_by'];
$order_asc = $_SESSION['photo_order_asc'];

$search_title = $_GET['search_title'];
$search_tag = $_GET['search_tag'];
$search_description = $_GET['search_description'];

$category_id = intval($_GET['category_id']);



function del_image($id)
{
	global $obj;
	$data = $obj->get_one($id);
	$obj->delete($id);
	@unlink($data['file_path']);
	@unlink($data['small']);
	@unlink($data['big']);
}


if($m == 'delete')
{
	if ($id)
	{
		del_image($id);
	}
	else if ($ids)
	{
		$arr = explode(',',$ids);
		foreach($arr as $id)
		{
			$id = intval($id);
			$id && del_image($id);
		}
	}
	$from_url = $_SESSION['last_list_page'];
	!$from_url && $from_url = "admin.php?p=photo_published&time={$time}&page={$page}";
	header("location:$from_url");
	die;
}

if ($time == 'all' || !$time)
{
	$from_time = false;
}
else if (intval($time))
{
	$from_time = time() - $time*60;
}


if ($from && $to)
{
	preg_match('/(\d+)-(\d+)-(\d+)/',$from,$matches);
	if (count($matches) == 4)
	{
		$from_time = mktime(0,0,0,$matches[2],$matches[3],$matches[1]);
		preg_match('/(\d+)-(\d+)-(\d+)/',$to,$matches);
		if (count($matches) == 4)
		{
			$to_time = mktime(23,59,59,$matches[2],$matches[3],$matches[1]);
		}
	}
}

$total = $obj->get_list( array(
	'select' => 'count(photo_id)',
	'status' => 1,
	'from_time' => $from_time,
	'to_time' => $to_time ,
	'title'=>$search_title,
	'description'=>$search_description,
	'tag'=>$search_tag,
	'category_id'=>$category_id));
$total = $total[0]['count(photo_id)'];
$page = intval($_GET['page']);
$per_page = (intval($config['published_image_per_page']))?intval($config['published_image_per_page']):50;
!$page && $page=1;
$total_page = ceil($total/$per_page);
!$total_page && $total_page=1;
$page>$total_page && $page = $total_page;
$image_list = $obj->get_list( array(
	'from' => ($page-1)*$per_page,
	'total'=>$per_page,
	'status' => 1,
	'from_time'=>$from_time,
	'to_time'=>$to_time,
	'order' => $order_by,
	'asc' => $order_asc,
	'title'=>$search_title,
	'description'=>$search_description,
	'tag'=>$search_tag,
	'category_id'=>$category_id));

$pager = PAGER_TOTAL.$total.PAGER_ITEMS.$total_page.PAGER_PAGE.'<br />';
$pager.= html_pager("admin.php?p=photo_published&time=$time",$total_page,$page);

$_SESSION['last_list_page'] = "admin.php?p=photo_published&page=$page&time=$time";
$view_data['page'] = $page;
$view_data['time'] = $time;
$view_data['from'] = $_GET['from'];
$view_data['to'] = $_GET['to'];
$view_data['keyword'] = $_GET['keyword'];
$view_data['pager'] = $pager;
$view_data['image_list'] = $image_list;
$view_data['page_description'] = PUBLISHED_IMAGES;
$view_data['order_by'] = $_SESSION['photo_order_by'];
$view_data['order_asc'] = $_SESSION['photo_order_asc'];
$view_data['search_title']=$search_title;
$view_data['search_description']=$search_description;
$view_data['search_tag']=$search_tag;

include_once('model/photo_category.php');
$category = new LZ_Photo_Category;
$view_data['categories'] = $category->get_list();
$view_data['category_id'] = $category_id;

$view_data['recent_time'] = $time;
?>
